/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmlwriter;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
import java.util.Iterator;
import javax.xml.stream.events.Attribute;

/**
 *
 * @author Oscar
 */
public class CommandBuilder {


    XMLOutputFactory outputFactory;
    XMLEventWriter eventWriter;

    /**
     * Contructor
     *
     * @param socket Socket que se utilizara para escribir el mensaje
     *
     * @throws XMLStreamException
     * @throws IOException
     */
    public CommandBuilder(Socket socket) throws XMLStreamException, IOException {

        // Se instancia la XMLOutputFactory
        this.outputFactory = XMLOutputFactory.newInstance();

        // Se instancia el XMLEventWriter
        this.eventWriter = outputFactory
                    .createXMLEventWriter(socket.getOutputStream());
    }

    /**
     * Crea un mensaje de Autenticacion de un Usuario para ser enviado al
     * servidor.
     *
     * Mensaje generado:
     * <?xml version="1.0" encoding="UTF-8"?>
     * <authenticate nickname="[nickname]" password="[password]"></authenticate>
     *
     * Donde [nickname] y [password] son placeholders de los parametros
     * introducidos
     *
     * @param nickname Nickaname introducido por el usuario
     * @param password Clave introducida por el usuario
     * @return
     * @throws XMLStreamException
     */
    public XMLEventWriter BuildAuthenticateMessage(String nickname, String password) throws XMLStreamException {

        // Create a EventFactory
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();

        XMLEvent end = eventFactory.createDTD("\n");

        // Create and write Start Tag
        eventWriter.add(eventFactory.createStartDocument());
        eventWriter.add(end);

        //Se crean los atributos del tag
        ArrayList<Attribute> attributes = new ArrayList<Attribute>();
        attributes.add(eventFactory.createAttribute("nickname", nickname));
        attributes.add(eventFactory.createAttribute("password", password));

        eventWriter.add(eventFactory.createStartElement
            ("", "", "authenticate",attributes.iterator(), null));

        eventWriter.add(eventFactory.createEndElement("", "", "authenticate"));

        eventWriter.add(eventFactory.createEndDocument());

        return eventWriter;
    }

    public XMLEventWriter BuildRegisterMessage(String nickname, String password) throws XMLStreamException {

        // Create a EventFactory
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();

        XMLEvent end = eventFactory.createDTD("\n");

        // Create and write Start Tag
        eventWriter.add(eventFactory.createStartDocument());
        eventWriter.add(end);

        //Se crean los atributos del tag
        ArrayList<Attribute> attributes = new ArrayList<Attribute>();
        attributes.add(eventFactory.createAttribute("nickname", nickname));
        attributes.add(eventFactory.createAttribute("password", password));

        eventWriter.add(eventFactory.createStartElement
            ("", "", "register",attributes.iterator(), null));

        eventWriter.add(eventFactory.createEndElement("", "", "register"));

        eventWriter.add(eventFactory.createEndDocument());

        return eventWriter;
    }


    public XMLEventWriter BuildGetLoadMessage() throws XMLStreamException {

        // Create a EventFactory
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();

        XMLEvent end = eventFactory.createDTD("\n");

        // Create and write Start Tag
        eventWriter.add(eventFactory.createStartDocument());
        eventWriter.add(end);

//        //Se crean los atributos del tag
//        ArrayList<Attribute> attributes = new ArrayList<Attribute>();
//        attributes.add(eventFactory.createAttribute("value", yesOrNo));

        eventWriter.add(eventFactory.createStartElement("", "", "getload"));//,attributes.iterator(), null));

        eventWriter.add(eventFactory.createEndElement("", "", "getload"));

        eventWriter.add(eventFactory.createEndDocument());

        return eventWriter;
    }

    public XMLEventWriter BuildGetFilesMessage() throws XMLStreamException {

        // Create a EventFactory
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();

        XMLEvent end = eventFactory.createDTD("\n");

        // Create and write Start Tag
        eventWriter.add(eventFactory.createStartDocument());
        eventWriter.add(end);

//        //Se crean los atributos del tag
//        ArrayList<Attribute> attributes = new ArrayList<Attribute>();
//        attributes.add(eventFactory.createAttribute("value", yesOrNo));

        eventWriter.add(eventFactory.createStartElement("", "", "getfiles"));//,attributes.iterator(), null));

        eventWriter.add(eventFactory.createEndElement("", "", "getfiles"));

        eventWriter.add(eventFactory.createEndDocument());

        return eventWriter;
    }

    public XMLEventWriter BuildDownloadMessage(String user,String name,
            String block) throws XMLStreamException {

        // Create a EventFactory
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();

        XMLEvent end = eventFactory.createDTD("\n");

        // Create and write Start Tag
        eventWriter.add(eventFactory.createStartDocument());
        eventWriter.add(end);

        //Se crean los atributos del tag
        ArrayList<Attribute> attributes = new ArrayList<Attribute>();
        attributes.add(eventFactory.createAttribute("user", user));
        attributes.add(eventFactory.createAttribute("name", name));
        attributes.add(eventFactory.createAttribute("block", block));

        eventWriter.add(eventFactory.createStartElement("", "", "download",attributes.iterator(), null));

        eventWriter.add(eventFactory.createEndElement("", "", "download"));

        eventWriter.add(eventFactory.createEndDocument());

        return eventWriter;
    }

    /**
     * Crea un mensaje de Autenticacion de un Usuario para ser enviado al
     * servidor.
     *
     * Mensaje generado:
     * <?xml version="1.0" encoding="UTF-8"?>
     * <response value="[yesOrNo]"></response>
     *
     * Donde [yesOrNo] es placeholder del parametros introducido
     *
     * @param nickname Nickaname introducido por el usuario
     * @param password Clave introducida por el usuario
     * @return
     * @throws XMLStreamException
     */
    public XMLEventWriter BuildResponseMessage(String yesOrNo) throws XMLStreamException {

        // Create a EventFactory
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();

        XMLEvent end = eventFactory.createDTD("\n");

        // Create and write Start Tag
        eventWriter.add(eventFactory.createStartDocument());
        eventWriter.add(end);

        //Se crean los atributos del tag
        ArrayList<Attribute> attributes = new ArrayList<Attribute>();
        attributes.add(eventFactory.createAttribute("value", yesOrNo));

        eventWriter.add(eventFactory.createStartElement
            ("", "", "response",attributes.iterator(), null));

        eventWriter.add(eventFactory.createEndElement("", "", "response"));

        eventWriter.add(eventFactory.createEndDocument());

        return eventWriter;
    }

}
